﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using ActiproSoftware.ComponentModel;
using ActiproSoftware.Drawing;
using ActiproSoftware.SyntaxEditor;
using PowerGui.AddOn.Interfaces;
using Quest.PowerGUI.ScriptEditor.Shared;

namespace PowerGui.AddOn.Impl
{
    class EditorFontsAndColors : IEditorFontAndColors
    {
        private readonly PSSyntaxEditorControl _psControl;
        public EditorFontsAndColors(PSSyntaxEditorControl psControl)
        {
            _psControl = psControl;
        }

        public Font Font
        {
            get { return _psControl.Font; }
            set { _psControl.Font = value; }
        }

        public SyntaxColors ReservedKeyWord
        {
            get { return GetSyntaxColors(PowerGuiColors.ReservedWordStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.ReservedWordStyle); }
        }

        public SyntaxColors Operator
        {
            get { return GetSyntaxColors(PowerGuiColors.OperatorStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.OperatorStyle); }
        }

        public SyntaxColors OperatorWord
        {
            get { return GetSyntaxColors(PowerGuiColors.OperatorWordStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.OperatorWordStyle); }
        }

        public SyntaxColors Variable
        {
            get { return GetSyntaxColors(PowerGuiColors.VariableStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.VariableStyle); }
        }

        public SyntaxColors Cmdlet
        {
            get { return GetSyntaxColors(PowerGuiColors.CmdletStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.CmdletStyle); }
        }

        public SyntaxColors DotNetStaticClass
        {
            get { return GetSyntaxColors(PowerGuiColors.NetClassStaticStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.NetClassStaticStyle); }
        }

        public SyntaxColors DotNetStaticClassMethod
        {
            get { return GetSyntaxColors(PowerGuiColors.NetClassStaticMethodStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.NetClassStaticMethodStyle); }
        }

        public SyntaxColors CmdletParameters
        {
            get { return GetSyntaxColors(PowerGuiColors.CmdletParamStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.CmdletParamStyle); }
        }

        public SyntaxColors Number
        {
            get { return GetSyntaxColors(PowerGuiColors.NumberStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.NumberStyle); }
        }

        public SyntaxColors StringDelimeter
        {
            get { return GetSyntaxColors(PowerGuiColors.StringDelimiterStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.StringDelimiterStyle); }
        }

        public SyntaxColors String
        {
            get { return GetSyntaxColors(PowerGuiColors.StringDefaultStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.StringDefaultStyle); }
        }

        public SyntaxColors CommentDelimeter
        {
            get { return GetSyntaxColors(PowerGuiColors.CommentDelimiterStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.CommentDelimiterStyle); }
        }

        public SyntaxColors Comment
        {
            get { return GetSyntaxColors(PowerGuiColors.CommentDefaultStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.CommentDefaultStyle); }
        }

        public SyntaxColors AutoVars
        {
            get { return GetSyntaxColors(PowerGuiColors.AutoVars); }
            set { SetSyntaxColor(value, PowerGuiColors.AutoVars); }
        }

        public SyntaxColors Function
        {
            get { return GetSyntaxColors(PowerGuiColors.FunctionStyle); }
            set { SetSyntaxColor(value, PowerGuiColors.FunctionStyle); }
        }

        public SyntaxColors Default
        {
            get { return GetSyntaxColors(PowerGuiColors.DefaultStyle); }
            set
            {
                SetSyntaxColor(value, PowerGuiColors.DefaultStyle);
                if (_psControl.RendererResolved is VisualStudio2005SyntaxEditorRenderer)
                {
                    var renderer = (_psControl.RendererResolved as VisualStudio2005SyntaxEditorRenderer);

                    renderer.TextAreaBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                    renderer.LineNumberMarginBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                    renderer.ScrollBarBlockBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                    renderer.UserMarginBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                    renderer.WordWrapMarginBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                    renderer.SplitterBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                    renderer.SelectionMarginBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                    renderer.IndicatorMarginBackgroundFill = new SolidColorBackgroundFill(value.BackgroundColor);
                }
            }
        }

        private SyntaxColors GetSyntaxColors(string colorName)
        {
            var index = _psControl.Language.HighlightingStyles.IndexOf(colorName);
            var style = _psControl.Language.HighlightingStyles[index];

            return new SyntaxColors(style.ForeColor, style.BackColor, style.Bold == DefaultableBoolean.True, style.Italic == DefaultableBoolean.True);
        }

        private void SetSyntaxColor(SyntaxColors colors, string colorName)
        {
            var index = _psControl.Language.HighlightingStyles.IndexOf(colorName);
            var style = _psControl.Language.HighlightingStyles[index];

            style.ForeColor = colors.ForegroundColor;
            style.BackColor = colors.BackgroundColor;
            style.Bold = colors.Bold ? DefaultableBoolean.True : DefaultableBoolean.False;
            style.Italic = colors.Italic ? DefaultableBoolean.True : DefaultableBoolean.False;
        }

        private static class PowerGuiColors
        {
            public const string ReservedWordStyle = "ReservedWordStyle";
            public const string OperatorStyle = "OperatorStyle";
            public const string OperatorWordStyle = "OperatorWordStyle";
            public const string VariableStyle = "VariableStyle";
            public const string CmdletStyle = "CmdletStyle";
            public const string NetClassStaticStyle = "NetClassStaticStyle";
            public const string NetClassStaticMethodStyle = "NetClassStaticMethodStyle";
            public const string CmdletParamStyle = "CmdletParamStyle";
            public const string NumberStyle = "NumberStyle";
            public const string StringDelimiterStyle = "StringDelimiterStyle";
            public const string StringDefaultStyle = "StringDefaultStyle";
            public const string CommentDelimiterStyle = "CommentDelimiterStyle";
            public const string CommentDefaultStyle = "CommentDefaultStyle";
            public const string AutoVars = "AutoVars";
            public const string FunctionStyle = "FunctionStyle";
            public const string DefaultStyle = "DefaultStyle";
        }
    }
}
